﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;
using DevExpress.XtraTreeList.Nodes;
using FromUtils;
using DevExpress.XtraTreeList;

namespace HTTPRequest2.From.Address
{
    public partial class AddressEditor : UserControl
    {
        /// <summary>
        /// 数据结构
        /// </summary>
        private RequestParameteData rpd;

        /// <summary>
        /// 当前node
        /// </summary>
        private TreeListNode currentNode;

        public AddressEditor()
        {
            InitializeComponent();
            
        }

        public void BuildAddressEditor(RequestParameteData _rpd)
        {
            this.rpd = _rpd;
            this.treeList1.DataBindings.Add("DataSource", rpd, "urls");
        }

        private void treeList1_MouseClick(object sender, MouseEventArgs e)
        {
            TreeListNode tn = TreeListTool.GetFocusNode(sender, e.Location);
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                currentNode = tn;
                if (tn != null)
                {
                    this.popupMenu1.ItemLinks[0].Visible = false;
                    this.popupMenu1.ItemLinks[1].Visible = true;
                }
                else
                {
                    this.popupMenu1.ItemLinks[0].Visible = true;
                    this.popupMenu1.ItemLinks[1].Visible = false;
                }
                this.popupMenu1.ShowPopup(MousePosition);
            }
        }

        private void barButtonItem_添加_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            AETool.SetAppendNode(this.treeList1, null, "");
        }

        private void barButtonItem_删除_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            AETool.SetDelete(this.treeList1, currentNode);
        }

        
    }

    static class AETool
    {

        /// <summary>
        /// 添加节点
        /// </summary>
        /// <param name="type"></param>
        public static void SetAppendNode(TreeList tl, TreeListNode tln, String type)
        {
            if (tln != null)
            {
                tl.AppendNode(new object[] { "新建地址", "", type, "", "", GUID.getGUID(), "" }, tln);
            }
            else
            {
                List<ParameterItemNodeData> NodeDatalist = (List<ParameterItemNodeData>)tl.DataSource;
                NodeDatalist.Add(
                    new ParameterItemNodeData()
                    {
                        Key = "新建地址",
                        Value = "http://localhost/",
                        Remark = "",
                        Type = "",
                        Parameter = "",
                        ID = GUID.getGUID(),
                        ParentID = "",
                    });

                tl.RefreshDataSource();
            }
            tl.ExpandAll();
        }

        /// <summary>
        /// 删除节点
        /// </summary>
        public static void SetDelete(TreeList tl, TreeListNode tln)
        {
            tl.DeleteNode(tln);
        }

    }
}
